import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class 回文数组 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int len = scan.nextInt();
        long[] arr = new long[len];
        for ( int i = 0 ; i < len ; i++ ) {
            arr[i] = scan.nextLong();
        }
        long count = 0;//计数器
        for (int i = 0 ; i < len/2 ; i++ ) {
            if (arr[i] < arr[len - i - 1]) {
                long gap1 = arr[len - i - 1] - arr[i];
                long gap2 = arr[len - i - 2] - arr[i + 1];
                arr[i] += gap1;
                if (arr[i + 1] < arr[len - i - 2]) {
                    arr[i + 1] += Math.min(gap1,gap2);
                }
                count += gap1;
            } else if (arr[i] > arr[len - i - 1]) {
                long gap1 = arr[i] - arr[len - i - 1];
                long gap2 = arr[i + 1] - arr[len - i - 2];
                arr[i] -= gap1;
                if (arr[i + 1] > arr[len - i - 2]) {
                    arr[i + 1] -= Math.min(gap1,gap2);
                }
                count += gap1;
            }
        }
        System.out.println(count);
        scan.close();
    }
}